Skip to main content
Version: 1.0.0

weeklySkuAssigned

Weekly SKU Assigned Template

Function Names: weeklySkuAssigned / testWeeklySkuAssigned

Author: Domenico Cerone Creation Date: 25/09/2025
Last Reviewer: Domenico Cerone

Trigger: Scheduler (onSchedule - every Friday at 18:00) / HTTPS (onRequest)

Purpose: Sends weekly notifications to modellers with the list of SKUs that have been assigned to them during the week.

Detailed Functionality

This Firebase Function performs the following operations in sequence:

1. Supervisor Retrieval

  • Queries the Profiles collection to get all users with ModellerSupervisor role
  • Extracts supervisor emails that will be CC'd in notifications

2. Weekly Period Calculation

  • Determines the start of the current week (Monday at 00:00:00)
  • Properly handles the Sunday case to calculate the previous Monday

3. Assignment Log Analysis

  • Retrieves all documents from the LogVariant collection
  • For each log, analyses the list_variant_assigned array to find the last assignment
  • Filters only assignments that occurred in the current week
  • Groups SKUs by assigned modeller

4. Data Enrichment

  • For each assigned SKU, retrieves data from the Variants collection to obtain:
    • mainBrandRef
    • productRef
    • variantId
  • This data is used to build validation links

5. User Profile Retrieval

  • For each modeller, attempts to retrieve the profile from the Profiles collection
  • If found, uses firstName and lastName for the full name
  • Otherwise, formats the username from the email as fallback

6. NOTIFICATION PREFERENCE CHECK

  • NEW FEATURE: Before sending any email, the function checks the recipient's profile for notification preferences
  • Check Field: notification_types.weeklySkuAssigned in the user's profile
  • Logic: If notification_types.weeklySkuAssigned is false, the email is NOT sent
  • Default Behavior: If the field is missing or true, the email is sent normally
  • Granular Control: Users can enable/disable specific notification types without affecting others
  • Profile Field: notification_types is a map containing boolean values for each notification type
  • Example: notification_types: { weeklySkuAssigned: false } disables this notification

7. Email Preparation and Sending

  • Builds an HTML table with assigned SKUs containing:
    • SKU
    • Product name
    • Validation link (format: http://localhost:3000/framevalidation/{mainBrandRef}/{productRef}/{variantId})
  • Uses ZeptoMail with predefined template (MAIL_TEMPLATE_KEY: 13ef.8598f19fbcc5adb.k1.b09af280-4609-11f0-900d-dad70ff08860.1975a4e91a8)
  • Includes in the template:
    • countSku: total number of assigned SKUs
    • list: HTML table of SKUs
    • accountName: modeller's full name
  • CCs all ModellerSupervisors

8. Error Handling and Fallbacks

  • In case of ZeptoMail error, uses nodemailer as fallback
  • Sends a simplified text version of the email
  • Records all results (success/error) for each modeller

9. Email Configuration

  • Sender: no-reply@arshades.com (ARShades Studio)
  • SMTP Server: smtp.zeptomail.eu:587
  • Template: ZeptoMail template system for formatted emails
  • Time Zone: Europe/Rome for scheduling

10. Scheduling

  • Main function: Runs automatically every Friday at 18:00
  • Test function: Available as HTTP endpoint for manual testing
  • Region: europe-central2

The function ensures that each modeller receives a complete and formatted notification with all SKUs that have been assigned to them during the week, facilitating workload monitoring and priority management.

Input (Payload):

For the scheduled function (weeklySkuAssigned):

No input required - automatically triggered by Firebase Scheduler every Friday at 18:00

For the HTTP test function (testWeeklySkuAssigned):

No payload required - simple GET request without parameters

Output (Success):

For the scheduled function (weeklySkuAssigned):

Output visible only in Cloud Functions Logs on Google Cloud Console
The function returns null but logs results in the format:
"Weekly SKU assignment report completed: [results array]"

For the HTTP test function (testWeeklySkuAssigned):

{
"success": true,
"message": "Weekly SKU assignment report completed successfully",
"results": [
{
"email": "modellatore@arshades.com",
"skuCount": 5,
"success": true
}
]
}

This output is returned in the HTTP response (Postman, browser, curl)

Testing

URL (if HTTPS): http://127.0.0.1:5001/arshadesstaging/us-central1/testWeeklySkuAssigned

Test with Emulator:

  1. Start the Firebase emulator: firebase emulators:start --only functions
  2. Ensure you're using Node.js version 20: nvm use 20
  3. Test with curl: curl "http://127.0.0.1:5001/arshadesstaging/us-central1/testWeeklySkuAssigned"

Postman Testing:

Deploy Command:

To deploy the scheduled function:

firebase deploy --only functions:weeklySkuAssigned

This is the main function that automatically triggers every Friday at 18:00

To deploy the test function (optional):

firebase deploy --only functions:testWeeklySkuAssigned

This function is used only for testing and is normally not deployed

To deploy both:

firebase deploy --only functions:weeklySkuAssigned,functions:testWeeklySkuAssigned

Production URL:

Live Function (Scheduled): https://europe-central2-arshades-7e18a.cloudfunctions.net/weeklySkuAssigned

Note: This function runs automatically every Friday at 18:00. The URL is for reference only as scheduled functions cannot be called directly.